<?php
App::uses('AppController', 'Controller');

class CurriculosController extends AppController {

	public $uses = array();
        function beforeFilter() {
            parent::beforeFilter();
            $AppInformation['Controller']['nameSingular'] = "Curriculoe";
            $AppInformation['Controller']['namePlural'] = "Curriculoes";
            $this->set("AppInformation",$AppInformation);

						$this->response->disableCache();
        }

        function index()
        {
            $this->layout = "admin";
						$id_logado = $this->Auth->user('id');
            $Curriculo = $this->Curriculo->find("first", array('conditions'=>array('Curriculo.user_id'=>$id_logado)));
						//die(debug($Curriculo['Curriculo']['status']));
            //$this->set('Curriculos',$Curriculos);
						if($Curriculo['User']['role']=="candidato"):
							$this->redirect(array('action'=>'edit'));
						else:
							$this->redirect(array('action'=>'listar'));
						endif;
        }

				function listar($filtro = null)
				{
					$filtro_original = $filtro;
					$this->set(compact('filtro_original'));
					switch ($filtro):
						case 'ator-atriz':
							$filtro = 'Ator/Atriz';
						break;
						case 'ator-atriz-mirim':
							$filtro = 'Ator/ Atriz mirim';
						break;
						case 'radio-ator':
							$filtro = 'Rádio-ator/atriz';
						break;
						case 'desenhista-ilustrador-layoutista':
							$filtro = 'Desenhista/ilustrador/layoutista';
						break;
						case 'radio-ator-atriz':
							$filtro = 'Rádio-ator/atriz';
						break;
						case 'radio-ator-atriz-mirim':
							$filtro = 'Rádio-ator/atriz mirim';
						break;
						case 'ator-bonequeiro':
	            $filtro = 'Ator/ Ator bonequeiro';
	          break;
					endswitch;


					$conditions = array('User.role'=>"candidato");
					$this->layout = "default";
					$this->Curriculo->Behaviors->load('Containable');
					$this->Curriculo->contain('User', 'Funcao');
					$Curriculos = $this->Curriculo->find("all", array('conditions'=>$conditions, 'order'=>'User.fullname ASC'));

					$total_curriculos = $this->Curriculo->find("count", array('conditions'=>$conditions));


					$funcoes = $this->Curriculo->Funcao->find("all");
					$funcoes_counting = array();
					$result = false;
					foreach($funcoes as $funcao):
						foreach($funcoes_counting as $funcao_count):
							if($funcao_count == array('curriculo_id'=>$funcao['Funcao']['curriculo_id'], 'funcao'=>$funcao['Funcao']['funcao'])):
								$result = true;
								break;
							else:
								$result = false;
							endif;
							//debug($result);
						endforeach;

						if(!$result):
							array_push($funcoes_counting, array('curriculo_id'=>$funcao['Funcao']['curriculo_id'], 'funcao'=>$funcao['Funcao']['funcao']));
							//echo $funcao['Funcao']['funcao'] . "<br/>";
						endif;
					endforeach;

					$funcoes_counted_array = array_count_values(array_map(function($item) {
					   	return $item['funcao'];
					}, $funcoes_counting));
					//die(debug($funcoes_counted_array));

					//die(debug($funcoes_counting));
					//die(debug($result));
					//die(debug(array_search(array('curriculo_id'=>37, 'funcao'=>'Assistente de direÃ§Ã£o'), $funcoes_counting)));
					//die(debug($Curriculos));
					$this->set('Curriculos',$Curriculos);
					$this->set('filtro',$filtro);
					$this->set('total_curriculos',$total_curriculos);
					$this->set(compact('funcoes_counted_array'));
				}


				function emails()
				{
					$this->layout = "admin";
					$conditions = array('Curriculo.status'=>1);
					$Curriculos = $this->Curriculo->find("all", array('conditions'=>$conditions));

					$this->set('Curriculos',$Curriculos);

					foreach($Curriculos as $Curriculo):
						if(empty($Curriculo['Formacao']) || empty($Curriculo['Funcao']) || empty($Curriculo['Curso'])):
							//echo('vazio');
							//echo('<pre>');
							if(!empty($Curriculo['User']['email'])):
								echo($Curriculo['User']['email']);
								echo(',');
							endif;
							//echo('</pre>');
						endif;
					endforeach;
					//die(debug($Curriculos));
					//die();
					//$this->redirect(array('action'=>'edit'));
				}

				//

				function json($tipo=null)
				{
					$this->layout = "ajax";


					switch($tipo){
						case "curriculo":
							$conditions = array('User.fullname'=>"", 'User.role'=>'candidato');
						break;

						case "telefones":
							$conditions = array('Curriculo.telefone_res'=>null, 'User.role'=>'candidato');
						break;

						default:
							$conditions = array('User.role'=>'candidato');
						break;
					}


					$this->layout = "default";
					$this->Curriculo->Behaviors->load('Containable');
					$this->Curriculo->contain('User', 'Formacao');
					$Curriculos = $this->Curriculo->find("all", array('conditions'=>$conditions, 'order'=>'User.fullname ASC'));

					$this->set(compact('Curriculos'));
					$this->set(compact('tipo'));
					//die(debug($Curriculos));

				}


        function add()
        {
            $this->layout = "default";

						//die(debug($this->inverteData("20/07/1992")));



            if ($this->request->is('post'))
            {
                //die(debug($this->request->data));


								//die(debug($this->Curriculo->find('all')));
                //$this->Curriculo->createWithAttachment($this->request->data['Curriculo']);

								$this->request->data['Curriculo']['user_id']=$this->Auth->user('id');

								//die(debug($this->request->data));
								$this->Curriculo->createWithAttachments($this->request->data);

								$id = $this->Curriculo->getLastInsertId();

								//die(debug($id));
								foreach($this->request->data['Formacao'] as $Formacao)
								{
									//die(debug($Formacao));
									$Formacao['curriculo_id']=$id;
									$this->Curriculo->Formacao->create();
										$this->Curriculo->Formacao->save($Formacao);
								}
								foreach($this->request->data['Curso'] as $Curso)
								{
									//die(debug($Formacao));
									$Curso['curriculo_id']=$id;
									$this->Curriculo->Curso->create();
										$this->Curriculo->Curso->save($Curso);
								}
								foreach($this->request->data['Portfolio'] as $Portfolio)
								{
									//die(debug($Formacao));
									$Portfolio['curriculo_id']=$id;
									$this->Curriculo->Portfolio->create();
										$this->Curriculo->Portfolio->save($Portfolio);
								}

								foreach($this->request->data['Experiencia'] as $Experiencia)
								{
									//die(debug($Formacao));
									$Experiencia['curriculo_id']=$id;
									$this->Curriculo->Experiencia->create();
									$this->Curriculo->Experiencia->save($Experiencia);
								}

								if(!empty($this->request->data['Funcao']))
								foreach($this->request->data['Funcao'] as $Funcao)
								{
									//die(debug($Formacao));
									$Funcao['curriculo_id']=$id;
									$this->Curriculo->Funcao->create();
									$this->Curriculo->Funcao->save($Funcao);
								}

								$this->request->data['User']['id'] = $this->Auth->user('id');
								$this->Curriculo->User->save($this->request->data['User']);

								$this->request->data['ExpArea']['curriculo_id']=$id;
								$this->Curriculo->ExpArea->save($this->request->data['ExpArea']);

								$this->request->data['ExpGenero']['curriculo_id']=$id;
								$this->Curriculo->ExpGenero->save($this->request->data['ExpGenero']);


								$this->request->data['ObsAtor']['curriculo_id']=$id;
								$this->Curriculo->ObsAtor->save($this->request->data['ObsAtor']);


								$this->request->data['ObsLocutor']['curriculo_id']=$id;
								$this->Curriculo->ObsLocutor->save($this->request->data['ObsLocutor']);

								//die(debug($id));
								//$this->redirect(array('action'=>'success'));
                $this->Session->setFlash("Salvo com sucesso!");

                // Trocar 2 por getInsertLastInsert
                //$this->redirect(array('action'=>'edit',2));
            }

						if($this->Auth->user())
						{
							$this->redirect(array('action'=>'edit'));
						}
        }



				function edit()
				{
					$this->layout = "default";

					//die(debug($this->inverteData("20/07/1992")));
					$user_id = $this->Auth->user('id');
					//die(debug($id));

					if ($this->request->is(array('post', 'put')))
					{



						//die(debug($this->Curriculo->find('all')));
						//$this->Curriculo->createWithAttachment($this->request->data['Curriculo']);

						$this->request->data['Curriculo']['user_id']=$this->Auth->user('id');
						$id = $this->request->data['Curriculo']['id'];
						//die(debug($this->request->data));
						$this->Curriculo->createWithAttachments($this->request->data);

						if(empty($id)):
							//die(debug('vazio'));
							$id = $this->Curriculo->getLastInsertId();
						endif;


						if(isset($this->request->data['Formacao']))
						foreach($this->request->data['Formacao'] as $Formacao)
						{
							//die(debug($Formacao));
							$Formacao['curriculo_id']=$id;
							$this->Curriculo->Formacao->create();
							$this->Curriculo->Formacao->save($Formacao);
						}

						if(isset($this->request->data['Curso']))
						foreach($this->request->data['Curso'] as $Curso)
						{
							//die(debug($Formacao));
							$Curso['curriculo_id']=$id;
							$this->Curriculo->Curso->create();
							$this->Curriculo->Curso->save($Curso);
						}

						if(isset($this->request->data['Portfolio']))
						foreach($this->request->data['Portfolio'] as $Portfolio)
						{
							//die(debug($Formacao));
							$Portfolio['curriculo_id']=$id;
							$this->Curriculo->Portfolio->create();
							$this->Curriculo->Portfolio->save($Portfolio);
						}

						if(isset($this->request->data['Experiencia']))
						foreach($this->request->data['Experiencia'] as $Experiencia)
						{
							//die(debug($Formacao));
							$Experiencia['curriculo_id']=$id;
							$this->Curriculo->Experiencia->create();
							$this->Curriculo->Experiencia->save($Experiencia);
						}

						$lista_funcao = array();
						if(!empty($this->request->data['Funcao']))
						foreach($this->request->data['Funcao'] as $Funcao)
						{
							array_push($lista_funcao, $Funcao['funcao']);
							$Funcao['curriculo_id']=$id;

							$search_funcao_curriculo = $this->Curriculo->Funcao->find('all', array('conditions'=>array('Funcao.funcao'=>$Funcao['funcao'], 'Funcao.curriculo_id'=>$Funcao['curriculo_id'])));

							$count_funcao = count($search_funcao_curriculo);
							if($count_funcao == 0){
								$this->Curriculo->Funcao->create();
								$this->Curriculo->Funcao->save($Funcao);
							}
							else
							{

							}
						}

						$search_deleted_funcoes = $this->Curriculo->Funcao->find('all',
							array('conditions'=>
								array('Funcao.curriculo_id'=>$this->request->data['Curriculo']['id'],
									"NOT"=>array('Funcao.funcao'=>$lista_funcao)),
							));

							foreach($search_deleted_funcoes as $funcao_to_delet):
								$this->Curriculo->Funcao->delete($funcao_to_delet['Funcao']['id']);
							endforeach;
							//die(debug($search_deleted_funcoes));


						$this->request->data['User']['id'] = $this->Auth->user('id');
						$this->Curriculo->User->save($this->request->data['User']);

						$this->request->data['ExpArea']['curriculo_id']=$id;
						$this->Curriculo->ExpArea->save($this->request->data['ExpArea']);

						$this->request->data['ExpGenero']['curriculo_id']=$id;
						$this->Curriculo->ExpGenero->save($this->request->data['ExpGenero']);


						$this->request->data['ObsAtor']['curriculo_id']=$id;
						$this->Curriculo->ObsAtor->save($this->request->data['ObsAtor']);


						$this->request->data['ObsLocutor']['curriculo_id']=$id;
						$this->Curriculo->ObsLocutor->save($this->request->data['ObsLocutor']);

						//die(debug($id));
						$this->redirect(array('action'=>'success'));
						$this->Session->setFlash("Salvo com sucesso!");

						// Trocar 2 por getInsertLastInsert
						//$this->redirect(array('action'=>'edit',2));
					}
					else
					{


					}
					$this->request->data = $this->Curriculo->find('first', array('conditions'=>array('User.id'=>$user_id)));
					//die(debug($this->request->data));
				}

				function ver($user_id=null)
				{
					$this->layout = "default";

					//die(debug($this->inverteData("20/07/1992")));

					//die(debug($id));

					$this->request->data = $this->Curriculo->find('first', array('conditions'=>array('User.id'=>$user_id)));


					$nextCurriculo = $this->Curriculo->find('first', array('conditions'=>array('Curriculo.id >'=>$this->request->data['Curriculo']['id'], 'Curriculo.status'=>1)));
					$previousCurriculo = $this->Curriculo->find('first', array('order'=>'Curriculo.id DESC', 'conditions'=>array('Curriculo.id <'=>$this->request->data['Curriculo']['id'], 'Curriculo.status'=>1)));
					$this->set(compact('nextCurriculo'));
					$this->set(compact('previousCurriculo'));

					//die(debug($nextCurriculo));
					//die(debug($this->request->data));
				}


				function success()
				{
					$this->layout = "default";
				}


        function delete($id)
        {
            $this->Propose->delete($id);
            $this->Session->setFlash('Removido com sucesso.', 'default', array(), 'good');
            $this->redirect('/admin/pages');
        }
}
